#include <iostream>
#include <sstream>
#include "Graph.h"         
#include "GUI.h"
#include "Window.h"
#include "Checkerboard.h"


using namespace Graph_lib;
using namespace std;
   
//create colors
Color maroon(fl_rgb_color(128,0,0)); 
Color burnt_orange(fl_rgb_color(204,85,0));
Color dark_green(fl_rgb_color(0,102,0));
Color green_yellow(fl_rgb_color(173,255,47));

int moves = 0;  //stores the number of moves made.
int posi = 0; //stores the position (from 0-31 basically) of the player's chip
int pickaggie = 0; //the aggie currently selected
vector<int> aggie_posi(4,0);
vector<int> ag_moves (4,0);
int a = 0;
int b = 0;
string as;
string bs;

Vector_ref<Rectangle> vr;
Vector_ref<Circle> cr;
Vector_ref<Circle> cr2;
string name3 = "scores.txt";
ofstream ofs;



void Checkerboard::save_name()
{
	ofs.open(name3.c_str(), ios::out | ios::app);
	string x = namein.get_string();
	ofs << x << " 100 " << endl; 
	ofs.close();

}

void Checkerboard::delete_ze_image() 			//remove the previously selected checker
{
	detach(pieces[posi]);
	redraw();
}
void Checkerboard::delete_ze_aggies(int c)		//^^
{
	detach(aggies[aggie_posi[c]]);
	redraw();
}


void Checkerboard::aggie_ai()					
{
	pickaggie = randint(0,4);

	if (ag_moves[pickaggie] % 2)
	{
		
		attach(aggies[aggie_posi[pickaggie]+5]);
		delete_ze_aggies(pickaggie);
		aggie_posi.at(pickaggie) =	aggie_posi[pickaggie]+5;
		ag_moves.at(pickaggie) = ag_moves[pickaggie] + 1;
			
	}
	else
	{

	attach(aggies[aggie_posi[pickaggie]-4]);
	delete_ze_aggies(pickaggie);
	aggie_posi.at(pickaggie) =	aggie_posi[pickaggie]-4;
	ag_moves.at(pickaggie) = ag_moves[pickaggie] + 1;
	}
	
}

void Checkerboard::winner()
{

	detach(board_image);
	
	for(int det = 0; det < 32; det++)
	{
	detach(aggies[det]);
	detach(pieces[det]);
	detach(pr[det]);
	}
	attach(winner_button);
	attach(winner_image);
	
}

int Checkerboard::check_conflict(int pos)
{
	for(int m = 0; m < 4; m++)
	{
		if(pos == aggie_posi[m])
		return 1;
	}
	return 0;
}
//alright, this function gets a bit crazy, so stick with me
void Checkerboard::pushed(Address b1)
{	
	for (int n = 0; n < 32; n++){ 							//loop through all 32 slots on the board

	int y = check_conflict(n);	
	if (y == 0){
	if (ar[n] == b1 && moves == 0){							//when the address is matched to the board, and it's the first move
	
		if (n == 3 || n == 11 || n == 19 || n == 27)		//only allow placement on the bottem tiles, i.e. tile 3,11,19,27
		{
			attach(pieces[n]);
			moves++;
			delete_ze_image();
			posi = n;
		}
		else												//otherwise, tell the user they failed.
		{
			cerr << "Please click on an appropriate square!\n";
		}
	}
	if (ar[n] == b1 && moves > 0 && n != posi)							//when the address is matched to a tile. and it's mid-game
		if(moves % 2){																//if the piece is currently on a type 1 row
			if(n == (posi-3) || n == (posi-4) ||n == (posi + 4)|| n == (posi + 5) ){	//and the click is a surrounding tile
				attach(pieces[n]);	//attach the checker
				moves++;			//add 1 to moves
				delete_ze_image();  //call detach function
				posi = n;			//set the current position
				aggie_ai();
			}
			}			
		else{																		//if the piece is currently on a type 2 row
			if(n == (posi-4) || n == (posi-5) ||n == (posi +3)|| n == (posi +4)){	//and the click is a surrounding tile
				attach(pieces[n]);  //See above
				moves++;
				delete_ze_image();
				posi = n;
				aggie_ai();
			}
			}
	
	}
	if(y == 1)
	{
	}
		
		}
	if (posi == 4 || posi == 12 || posi == 20 || posi == 28)
	{
		winner();
	}

}

void setup_vector()
{
   aggie_posi.at(0)=4;
   aggie_posi.at(1)=12;
   aggie_posi.at(2)=20;
   aggie_posi.at(3)=28;

}
int main()
try
{	



	
	setup_vector();
	Checkerboard win1(Point(100,100),640,480,"Checkerboard");
	return gui_main();
	
}
catch(exception& e) {
    cerr << "exception" << e.what() << "\n";
    return 1;
}
catch(...) {
	cerr << "Some exception!1!!!1\n";
    return 2;
}
